package com.itheima.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.common.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@Configuration
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpSession session;

    /**
     * 插入操作 公共字段填充数据设置
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充：【insert】");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        /*metaObject.setValue("createUser",1L);
        metaObject.setValue("updateUser",1L);*/

        //方案一：使用session获取登录员工id
        /*Long empId = (Long) session.getAttribute("employee");
        log.info("empId:{}",empId);

        metaObject.setValue("createUser",empId);
        metaObject.setValue("updateUser",empId);*/

        //方案二：使用ThreadLocal获取当前登录的员工id
        metaObject.setValue("createUser", BaseContext.get());
        metaObject.setValue("updateUser",BaseContext.get());
    }

    /**
     * 更新操作 公共字段填充数据设置
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充：【update】");

        log.info("MyMetaObjectHandler-当前线程id：{}",Thread.currentThread().getId());

        metaObject.setValue("updateTime",LocalDateTime.now());
        //metaObject.setValue("updateUser",1L);

        //方案一：session
        /*Long empId = (Long) session.getAttribute("employee");
        log.info("empId:{}",empId);
        metaObject.setValue("updateUser",empId);*/

        //方案二：ThreadLocal
        metaObject.setValue("updateUser",BaseContext.get());
    }
}
